package com.itheima.controller;

import com.itheima.Result.Result;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import com.itheima.service.impl.DeptServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理Controller
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/depts")
@Api(tags = "部门管理")
public class DeptController {

    private final DeptService deptService;

    /**
     * 查询全部部门
     * @return
     */
    @ApiOperation(value="获取所有部门信息")
    @GetMapping
    public Result<List<Dept>> getDepts(){
        log.info("查询全部部门数据");
        //调用service查询部门数据
        return Result.success(deptService.list());
    }

    /**
     * 根据id删除部门
     * @param id
     * @return
     */
    @ApiOperation(value="根据id删除部门")
    @DeleteMapping("/{id}")
    public Result delectDeptById(@ApiParam(name = "id",value = "部门id") @PathVariable("id")Integer id){
        log.info("根据id删除部门数据{}",id);
        deptService.delectDeptById(id);
        return Result.success();
    }

    /**
     * 根据id查询部门
     * @param id
     * @return
     */
    @ApiOperation(value="根据id查询部门")
    @GetMapping("/{id}")
    public Result<Dept> getDeptById(@ApiParam(name = "id",value = "部门id")@PathVariable("id")Integer id){
        log.info("根据id查询部门{}",id);
        return Result.success(deptService.getDeptById(id));
    }

    @ApiOperation(value="添加部门")
    @PostMapping()
    public Result addDept(@RequestBody Dept dept){
        log.info("添加部门数据{}",dept);
        deptService.addDept(dept);
        return Result.success();
    }



}
